package com.xiweicheng.test.jsonpath;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;

public class Main {
    public static void main(String[] args) {
        String json = "{\n" +
                "    \"store\": {\n" +
                "        \"book\": [\n" +
                "            {\n" +
                "                \"category\": \"reference\",\n" +
                "                \"author\": \"Nigel Rees\",\n" +
                "                \"title\": \"Sayings of the Century\",\n" +
                "                \"price\": 8.95\n" +
                "            },\n" +
                "            {\n" +
                "                \"category\": \"fiction\",\n" +
                "                \"author\": \"Evelyn Waugh\",\n" +
                "                \"title\": \"Sword of Honour\",\n" +
                "                \"price\": 12.99\n" +
                "            },\n" +
                "            {\n" +
                "                \"category\": \"fiction\",\n" +
                "                \"author\": \"Herman Melville\",\n" +
                "                \"title\": \"Moby Dick\",\n" +
                "                \"isbn\": \"0-553-21311-3\",\n" +
                "                \"price\": 8.99\n" +
                "            },\n" +
                "            {\n" +
                "                \"category\": \"fiction\",\n" +
                "                \"author\": \"J. R. R. Tolkien\",\n" +
                "                \"title\": \"The Lord of the Rings\",\n" +
                "                \"isbn\": \"0-395-19395-8\",\n" +
                "                \"price\": 22.99\n" +
                "            }\n" +
                "        ],\n" +
                "        \"bicycle\": {\n" +
                "            \"color\": \"red\",\n" +
                "            \"price\": 19.95\n" +
                "        }\n" +
                "    },\n" +
                "    \"expensive\": 10\n" +
                "}";

        // https://github.com/json-path/JsonPath
//        String title = JsonPath.read(json, "$.store.book[0].title");
//        System.out.println(title);

//        Object document = Configuration.defaultConfiguration().jsonProvider().parse(json);

//        String author0 = JsonPath.read(document, "$.store.book[0].author");
//        System.out.println(author0);
//        String author1 = JsonPath.read(document, "$.store.book[1].author");
//        System.out.println(author1);

//        Configuration configuration = Configuration.defaultConfiguration();
//        Configuration configuration1 = configuration.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
//        String title2 = JsonPath.using(configuration1).parse(json).read("$.store.book[0].title");
//        System.out.println(title2);

        String json2 = "[\n" +
                "   {\n" +
                "      \"name\" : \"john\",\n" +
                "      \"gender\" : \"male\"\n" +
                "   },\n" +
                "   {\n" +
                "      \"name\" : \"ben\"\n" +
                "   }\n" +
                "]";

        Configuration conf = Configuration.defaultConfiguration();

//Works fine
//        String gender0 = JsonPath.using(conf).parse(json).read("$[0]['gender']");
////PathNotFoundException thrown
//        String gender1 = JsonPath.using(conf).parse(json).read("$[1]['gender']");

        Configuration conf2 = conf.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);

//Works fine
//        String gender0 = JsonPath.using(conf2).parse(json2).read("$[0]['gender']");
////Works fine (null is returned)
//        String gender1 = JsonPath.using(conf2).parse(json2).read("$[1]['gender']");

        String title2 = JsonPath.using(conf2).parse(json).read("$.store.book[0].title1");
        System.out.println(title2);

    }
}
